/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.ui.components;

import rpg.RPG;
import rpg.gfx.Bitmap;

/**
 *
 * @author rothens
 */
public class ComponentTextfield extends Component {

    private String text = "";
    private String allowedChars;
    private Bitmap defaultText;
    private Bitmap background_inactive;
    private Bitmap background_active;
    private Bitmap background_hover;
    private Bitmap textArea;
    private int left;
    private int top;
    private int maxLength = -1;
    private boolean password = false;

    public ComponentTextfield(int width, int height, Integer x, Integer y, String allowedChars, Bitmap defaultText) {
        super(width, height, x, y);
        this.allowedChars = allowedChars;
        this.defaultText = defaultText;
        background_active = new Bitmap(width, height);
        background_active.clearWith(0xaa000001);
        background_active = StaticTextures.border_active.overlay(background_active);
        
        background_inactive = new Bitmap(width, height);
        background_inactive.clearWith(0xaa000001);
        background_inactive = StaticTextures.border_inactive.overlay(background_inactive);
        
        background_hover = new Bitmap(width, height);
        background_hover.clearWith(0xaa000001);
        background_hover = StaticTextures.border_hover.overlay(background_hover);
        left = StaticTextures.border_active.getLeftcol();
        top = StaticTextures.border_active.getToprow();
        textArea = new Bitmap(width - left - StaticTextures.border_active.getRightcol(), height - top - StaticTextures.border_active.getRightcol());
        redraw();
    }

    public ComponentTextfield(int width, int height, Integer x, Integer y, String allowedChars, Bitmap defaultText, int maxLength) {
        this(width, height, x, y, allowedChars, defaultText);
        this.maxLength = maxLength;
    }

    public ComponentTextfield(int width, int height, Integer x, Integer y, String allowedChars, Bitmap defaultText, boolean password) {
        this(width, height, x, y, allowedChars, defaultText);
        this.password = password;
    }

    public ComponentTextfield(int width, int height, Integer x, Integer y, String allowedChars, Bitmap defaultText, int maxLength, boolean password) {
        this(width, height, x, y, allowedChars, defaultText, maxLength);
        this.password = password;
    }

    public void append(String text) {
        for (Character c : text.toCharArray()) {
            if (allowedChars.contains(c.toString()) && (maxLength<0 || this.text.length()<maxLength)) {
                this.text += c;
                redraw();
            }
        }
    }

    public void backspace() {
        if (!text.isEmpty()) {
            text = text.substring(0, text.length() - 1);
            redraw();
        }
    }

    @Override
    public void redraw() {
        textArea.clear();
        if (text.isEmpty()) {
            textArea.draw(defaultText, 0, 0);
        } else {
            if (password) {
                textArea.draw(RPG.fs.getText(text.replaceAll(".", "*"), (byte) 2, 0xffffff), 0, 0);
            } else {
                textArea.draw(RPG.fs.getText(text, (byte) 2, 0xffffff), 0, 0);
            }
        }
        if(state == ComponentState.HOVER){
            draw(background_hover, 0, 0);
        } else if (state == ComponentState.SELECTED) {
            draw(background_active, 0, 0);
        } else {
            draw(background_inactive, 0, 0);
        }
        drawOn(textArea, left, top);
        if(parent != null){
            ((Component)parent).redraw();
        }
        //super.redraw();
    }

    public void clearText() {
        text = "";
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getMaxLength() {
        return maxLength;
    }

    public void setMaxLength(int maxLength) {
        this.maxLength = maxLength;
    }

    public boolean isPassword() {
        return password;
    }

    public void setPassword(boolean password) {
        this.password = password;
    }
}
